<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>biquad</title>
    <link rel="stylesheet" type="text/css" href="csound.css" />
    <meta name="generator" content="DocBook XSL Stylesheets V1.78.1" />
    <link rel="home" href="index.html" title="The Canonical Csound Reference Manual" />
    <link rel="up" href="OpcodesTop.html" title="Orchestra Opcodes and Operators" />
    <link rel="prev" href="binit.html" title="binit" />
    <link rel="next" href="biquada.html" title="biquada" />
  </head>
  <body>
    <div class="navheader">
      <table width="100%" summary="Navigation header">
        <tr>
          <th colspan="3" align="center">biquad</th>
        </tr>
        <tr>
          <td width="20%" align="left"><a accesskey="p" href="binit.html">Prev</a> </td>
          <th width="60%" align="center">Orchestra Opcodes and Operators</th>
          <td width="20%" align="right"> <a accesskey="n" href="biquada.html">Next</a></td>
        </tr>
      </table>
      <hr />
    </div>
    <div class="refentry">
      <a id="biquad"></a>
      <div class="titlepage"></div>
      <a id="IndexBiquad" class="indexterm"></a>
      <div class="refnamediv">
        <h2>
          <span class="refentrytitle">biquad</span>
        </h2>
        <p>biquad — 
      A sweepable general purpose biquadratic digital filter.
          </p>
      </div>
      <div class="refsect1">
        <a id="idp92126608"></a>
        <h2>Description</h2>
        <p>
      A sweepable general purpose biquadratic digital filter.
    </p>
      </div>
      <div class="refsect1">
        <a id="idp92127968"></a>
        <h2>Syntax</h2>
        <pre class="synopsis">ares <span class="command"><strong>biquad</strong></span> asig, kb0, kb1, kb2, ka0, ka1, ka2 [, iskip]</pre>
      </div>
      <div class="refsect1">
        <a id="idp92193760"></a>
        <h2>Initialization</h2>
        <p>
      <span class="emphasis"><em>iskip</em></span> (optional, default=0) -- if non-zero, intialization will be skipped. Default value 0. (New in Csound version 3.50)
    </p>
      </div>
      <div class="refsect1">
        <a id="idp92195584"></a>
        <h2>Performance</h2>
        <p>
      <span class="emphasis"><em>asig</em></span> -- input signal
    </p>
        <p>
      <span class="emphasis"><em>biquad</em></span> is a general purpose biquadratic digital filter of the form:
      </p>
        <div class="literallayout">
          <p><br />
  a0*y(n) + a1*y[n-1] + a2*y[n-2] = b0*x[n] + b1*x[n-1] + b2*x[n-2]<br />
      </p>
        </div>
        <p>
    </p>
        <p>
      This filter has the following frequency response:
      </p>
        <div class="literallayout">
          <p><br />
         B(Z)   b0 + b1*Z<sup>-1</sup>  + b2*Z<sup>-2</sup><br />
  H(Z) = ---- = ------------------<br />
         A(Z)   a0 + a1*Z<sup>-1</sup>  + a2*Z<sup>-2</sup><br />
      </p>
        </div>
        <p>
    </p>
        <p>
      This type of filter is often encountered in digital signal processing literature. It allows six user-defined k-rate coefficients.
    </p>
      </div>
      <div class="refsect1">
        <a id="idp92203456"></a>
        <h2>Examples</h2>
        <p>
      Here is an example of the biquad opcode. It uses the file <a class="ulink" href="examples/biquad.csd" target="_top"><em class="citetitle">biquad.csd</em></a>.

      </p>
        <div class="example">
          <a id="idp92205232"></a>
          <p class="title">
            <strong>Example 90. Example of the biquad opcode.</strong>
          </p>
          <div class="example-contents">
            <p>See the sections <a class="link" href="UsingRealTime.html" title="Real-Time Audio"><em class="citetitle">Real-time Audio</em></a> and <a class="link" href="CommandFlags.html" title="Csound command line"><em class="citetitle">Command Line Flags</em></a> for more information on using command line flags.</p>
            <pre class="programlisting">
<span class="csdtag">&lt;CsoundSynthesizer&gt;</span>
<span class="csdtag">&lt;CsOptions&gt;</span>
<span class="comment">; Select audio/midi flags here according to platform</span>
<span class="comment">; Audio out   Audio in    No messages</span>
-odac           -iadc     -d     <span class="comment">;;;RT audio I/O</span>
<span class="comment">; For Non-realtime ouput leave only the line below:</span>
<span class="comment">; -o biquad.wav -W ;;; for file output any platform</span>
<span class="csdtag">&lt;/CsOptions&gt;</span>
<span class="csdtag">&lt;CsInstruments&gt;</span>

<span class="comment">; Initialize the global variables.</span>
<span class="ohdr">sr</span> <span class="op">=</span> 44100
<span class="ohdr">kr</span> <span class="op">=</span> 4410
<span class="ohdr">ksmps</span> <span class="op">=</span> 10
<span class="ohdr">nchnls</span> <span class="op">=</span> 2

<span class="comment">; Instrument #1.</span>
<span class="oblock">instr</span> 1
  <span class="comment">; Get the values from the score.</span>
  idur <span class="op">=</span> p3
  iamp <span class="op">=</span> p4
  icps <span class="op">=</span> <span class="opc">cpspch</span>(p5)
  kfco <span class="op">=</span> p6
  krez <span class="op">=</span> p7

  <span class="comment">; Calculate the biquadratic filter's coefficients </span>
  kfcon <span class="op">=</span> 2<span class="op">*</span>3.14159265<span class="op">*</span>kfco<span class="op">/</span><span class="ohdr">sr</span>
  kalpha <span class="op">=</span> 1<span class="op">-</span>2<span class="op">*</span>krez<span class="op">*</span><span class="opc">cos</span>(kfcon)<span class="op">*</span><span class="opc">cos</span>(kfcon)<span class="op">+</span>krez<span class="op">*</span>krez<span class="op">*</span><span class="opc">cos</span>(2<span class="op">*</span>kfcon)
  kbeta <span class="op">=</span> krez<span class="op">*</span>krez<span class="op">*</span><span class="opc">sin</span>(2<span class="op">*</span>kfcon)<span class="op">-</span>2<span class="op">*</span>krez<span class="op">*</span><span class="opc">cos</span>(kfcon)<span class="op">*</span><span class="opc">sin</span>(kfcon)
  kgama <span class="op">=</span> 1<span class="op">+</span><span class="opc">cos</span>(kfcon)
  km1 <span class="op">=</span> kalpha<span class="op">*</span>kgama<span class="op">+</span>kbeta<span class="op">*</span><span class="opc">sin</span>(kfcon)
  km2 <span class="op">=</span> kalpha<span class="op">*</span>kgama<span class="op">-</span>kbeta<span class="op">*</span><span class="opc">sin</span>(kfcon)
  kden <span class="op">=</span> <span class="opc">sqrt</span>(km1<span class="op">*</span>km1<span class="op">+</span>km2<span class="op">*</span>km2)
  kb0 <span class="op">=</span> 1.5<span class="op">*</span>(kalpha<span class="op">*</span>kalpha<span class="op">+</span>kbeta<span class="op">*</span>kbeta)<span class="op">/</span>kden
  kb1 <span class="op">=</span> kb0
  kb2 <span class="op">=</span> 0
  ka0 <span class="op">=</span> 1
  ka1 <span class="op">=</span> <span class="op">-</span>2<span class="op">*</span>krez<span class="op">*</span><span class="opc">cos</span>(kfcon)
  ka2 <span class="op">=</span> krez<span class="op">*</span>krez
  
  <span class="comment">; Generate an input signal.</span>
  axn <span class="opc">vco</span> 1, icps, 1

  <span class="comment">; Filter the input signal.</span>
  ayn <span class="opc">biquad</span> axn, kb0, kb1, kb2, ka0, ka1, ka2
  <span class="opc">outs</span> ayn<span class="op">*</span>iamp<span class="op">/</span>2, ayn<span class="op">*</span>iamp<span class="op">/</span>2
<span class="oblock">endin</span>


<span class="csdtag">&lt;/CsInstruments&gt;</span>
<span class="csdtag">&lt;CsScore&gt;</span>

<span class="comment">; Table #1, a sine wave.</span>
<span class="stamnt">f</span> 1 0 16384 10 1

<span class="comment">;    Sta  Dur  Amp    Pitch Fco   Rez</span>
<span class="stamnt">i</span> 1  0.0  1.0  20000  6.00  1000  .8
<span class="stamnt">i</span> 1  1.0  1.0  20000  6.03  2000  .95
<span class="stamnt">e</span>


<span class="csdtag">&lt;/CsScore&gt;</span>
<span class="csdtag">&lt;/CsoundSynthesizer&gt;</span>
</pre>
          </div>
        </div>
        <p><br class="example-break" />
    </p>
        <p>
      Here is another example of the biquad opcode used for modal synthesis. It uses the file <a class="ulink" href="examples/biquad-2.csd" target="_top"><em class="citetitle">biquad-2.csd</em></a>. See the <a class="link" href="MiscModalFreq.html" title="Appendix E. Modal Frequency Ratios"><em class="citetitle">Modal Frequency Ratios</em></a> appendix for other frequency ratios.

      </p>
        <div class="example">
          <a id="idp92211600"></a>
          <p class="title">
            <strong>Example 91. Example of the biquad opcode for modal synthesis.</strong>
          </p>
          <div class="example-contents">
            <pre class="programlisting">
<span class="csdtag">&lt;CsoundSynthesizer&gt;</span>
<span class="csdtag">&lt;CsOptions&gt;</span>
<span class="comment">; Select audio/midi flags here according to platform</span>
<span class="comment">; Audio out   Audio in</span>
-odac           -iadc    <span class="comment">;;;RT audio I/O</span>
<span class="comment">; For Non-realtime ouput leave only the line below:</span>
<span class="comment">; -o biquad-2.wav -W ;;; for file output any platform</span>
<span class="csdtag">&lt;/CsOptions&gt;</span>
<span class="csdtag">&lt;CsInstruments&gt;</span>
<span class="ohdr">sr</span> <span class="op">=</span> 44100
<span class="ohdr">kr</span> <span class="op">=</span> 4410
<span class="ohdr">ksmps</span> <span class="op">=</span> 10
<span class="ohdr">nchnls</span> <span class="op">=</span> 2

<span class="op">/</span><span class="op">*</span>  modal synthesis using <span class="opc">biquad</span> filters as oscillators
    Example by Scott Lindroth 2007 <span class="op">*</span><span class="op">/</span>


<span class="oblock">instr</span> 1

    ipi <span class="op">=</span> 3.1415926
    idenom <span class="op">=</span> <span class="ohdr">sr</span><span class="op">*</span>0.5

    ipulseSpd <span class="op">=</span> p4
    icps     <span class="op">=</span> p5
    ipan <span class="op">=</span> p6
    iamp    <span class="op">=</span> p7
    iModes <span class="op">=</span> p8

    apulse    <span class="opc">mpulse</span> iamp, 0

    icps    <span class="op">=</span> <span class="opc">cpspch</span>( icps )

    <span class="comment">; filter gain</span>

    iamp1 <span class="op">=</span> 600
    iamp2 <span class="op">=</span> 1000
    iamp3 <span class="op">=</span> 1000
    iamp4 <span class="op">=</span> 1000
    iamp5 <span class="op">=</span> 1000
    iamp6 <span class="op">=</span> 1000

    <span class="comment">; resonance</span>

    irpole1 <span class="op">=</span> 0.99999
    irpole2 <span class="op">=</span> irpole1
    irpole3 <span class="op">=</span> irpole1
    irpole4 <span class="op">=</span> irpole1
    irpole5 <span class="op">=</span> irpole1
    irpole6 <span class="op">=</span> irpole1

    <span class="comment">; modal frequencies</span>

    <span class="octrl">if</span> (iModes <span class="op">==</span> 1) <span class="octrl">goto</span> modes1
    <span class="octrl">if</span> (iModes <span class="op">==</span> 2) <span class="octrl">goto</span> modes2
    
    modes1<span class="op">:</span>
    if1    <span class="op">=</span> icps <span class="op">*</span> 1            <span class="comment">;pot lid</span>
    if2    <span class="op">=</span> icps <span class="op">*</span> 6.27
    if3    <span class="op">=</span> icps <span class="op">*</span> 3.2
    if4    <span class="op">=</span> icps <span class="op">*</span> 9.92
    if5    <span class="op">=</span> icps <span class="op">*</span> 14.15
    if6    <span class="op">=</span> icps <span class="op">*</span> 6.23
    <span class="octrl">goto</span> nextPart

    modes2<span class="op">:</span>
    if1     <span class="op">=</span> icps <span class="op">*</span> 1            <span class="comment">;uniform wood bar</span>
    if2     <span class="op">=</span> icps <span class="op">*</span> 2.572
    if3     <span class="op">=</span> icps <span class="op">*</span> 4.644
    if4     <span class="op">=</span> icps <span class="op">*</span> 6.984
    if5     <span class="op">=</span> icps <span class="op">*</span> 9.723
    if6     <span class="op">=</span> icps <span class="op">*</span> 12.0
    <span class="octrl">goto</span> nextPart

    nextPart<span class="op">:</span>

    <span class="comment">; convert frequency to radian frequency</span>

    itheta1 <span class="op">=</span> (if1<span class="op">/</span>idenom) <span class="op">*</span> ipi
    itheta2 <span class="op">=</span> (if2<span class="op">/</span>idenom) <span class="op">*</span> ipi
    itheta3 <span class="op">=</span> (if3<span class="op">/</span>idenom) <span class="op">*</span> ipi
    itheta4 <span class="op">=</span> (if4<span class="op">/</span>idenom) <span class="op">*</span> ipi
    itheta5 <span class="op">=</span> (if5<span class="op">/</span>idenom) <span class="op">*</span> ipi
    itheta6 <span class="op">=</span> (if6<span class="op">/</span>idenom) <span class="op">*</span> ipi

    <span class="comment">; calculate coefficients</span>

    ib11 <span class="op">=</span> <span class="op">-</span>2 <span class="op">*</span> irpole1 <span class="op">*</span> <span class="opc">cos</span>(itheta1)
    ib21 <span class="op">=</span> irpole1 <span class="op">*</span> irpole1
    ib12 <span class="op">=</span> <span class="op">-</span>2 <span class="op">*</span> irpole2 <span class="op">*</span> <span class="opc">cos</span>(itheta2)
    ib22 <span class="op">=</span> irpole2 <span class="op">*</span> irpole2
    ib13 <span class="op">=</span> <span class="op">-</span>2 <span class="op">*</span> irpole3 <span class="op">*</span> <span class="opc">cos</span>(itheta3)
    ib23 <span class="op">=</span> irpole3 <span class="op">*</span> irpole3
    ib14 <span class="op">=</span> <span class="op">-</span>2 <span class="op">*</span> irpole4 <span class="op">*</span> <span class="opc">cos</span>(itheta4)
    ib24 <span class="op">=</span> irpole4 <span class="op">*</span> irpole4
    ib15 <span class="op">=</span> <span class="op">-</span>2 <span class="op">*</span> irpole5 <span class="op">*</span> <span class="opc">cos</span>(itheta5)
    ib25 <span class="op">=</span> irpole5 <span class="op">*</span> irpole5
    ib16 <span class="op">=</span> <span class="op">-</span>2 <span class="op">*</span> irpole6 <span class="op">*</span> <span class="opc">cos</span>(itheta6)
    ib26 <span class="op">=</span> irpole6 <span class="op">*</span> irpole6

    <span class="comment">;printk 1, ib 11</span>
    <span class="comment">;printk 1, ib 21</span>

    <span class="comment">;  also try setting the -1 coeff. to 0, but be sure to scale down the amplitude!</span>

    asin1     <span class="opc">biquad</span>  apulse <span class="op">*</span> iamp1, 1, 0, <span class="op">-</span>1, 1, ib11, ib21
         asin2       <span class="opc">biquad</span>  apulse <span class="op">*</span> iamp2, 1, 0, <span class="op">-</span>1, 1, ib12, ib22
         asin3       <span class="opc">biquad</span>  apulse <span class="op">*</span> iamp3, 1, 0, <span class="op">-</span>1, 1, ib13, ib23
         asin4       <span class="opc">biquad</span>  apulse <span class="op">*</span> iamp4, 1, 0, <span class="op">-</span>1, 1, ib14, ib24
         asin5       <span class="opc">biquad</span>  apulse <span class="op">*</span> iamp5, 1, 0, <span class="op">-</span>1, 1, ib15, ib25
         asin6       <span class="opc">biquad</span>  apulse <span class="op">*</span> iamp6, 1, 0, <span class="op">-</span>1, 1, ib16, ib26


    afin    <span class="op">=</span>    (asin1 <span class="op">+</span> asin2 <span class="op">+</span> asin3 <span class="op">+</span> asin4 <span class="op">+</span> asin5 <span class="op">+</span> asin6)

    <span class="opc">outs</span>        afin <span class="op">*</span> <span class="opc">sqrt</span>(p6), afin<span class="op">*</span><span class="opc">sqrt</span>(1<span class="op">-</span>p6)

<span class="oblock">endin</span>
<span class="csdtag">&lt;/CsInstruments&gt;</span>
<span class="csdtag">&lt;CsScore&gt;</span>
<span class="comment">;ins     st    dur  pulseSpd   pch    pan    amp     Modes</span>
<span class="stamnt">i</span>1        0    12    0       7.089    0      0.7    2
<span class="stamnt">i</span>1        .    .    .        7.09     1      .      .
<span class="stamnt">i</span>1        .    .    .        7.091    0.5    .      .

<span class="stamnt">i</span>1        0    12    0       8.039    0      0.7    2
<span class="stamnt">i</span>1        0    12    0       8.04     1      0.7    2
<span class="stamnt">i</span>1        0    12    0       8.041    0.5    0.7    2

<span class="stamnt">i</span>1        9    .    .        7.089    0      .      2
<span class="stamnt">i</span>1        .    .    .        7.09     1      .      .
<span class="stamnt">i</span>1        .    .    .        7.091    0.5    .      .

<span class="stamnt">i</span>1        9    12    0       8.019    0      0.7    2
<span class="stamnt">i</span>1        9    12    0       8.02     1      0.7    2
<span class="stamnt">i</span>1        9    12    0       8.021    0.5    0.7    2
<span class="stamnt">e</span>
<span class="csdtag">&lt;/CsScore&gt;</span>
<span class="csdtag">&lt;/CsoundSynthesizer&gt;</span>
</pre>
          </div>
        </div>
        <p><br class="example-break" />
    </p>
      </div>
      <div class="refsect1">
        <a id="idp92213536"></a>
        <h2>See Also</h2>
        <p>
      <a class="link" href="biquada.html" title="biquada"><em class="citetitle">biquada</em></a>,
      <a class="link" href="moogvcf.html" title="moogvcf"><em class="citetitle">moogvcf</em></a>,
      <a class="link" href="rezzy.html" title="rezzy"><em class="citetitle">rezzy</em></a>
    </p>
      </div>
      <div class="refsect1">
        <a id="idp92217568"></a>
        <h2>Credits</h2>
        <p>
      </p>
        <table border="0" summary="Simple list" class="simplelist">
          <tr>
            <td>Author: Hans Mikelson</td>
          </tr>
          <tr>
            <td>October 1998</td>
          </tr>
        </table>
        <p>
    </p>
        <p>New in Csound version 3.49</p>
      </div>
    </div>
    <div class="navfooter">
      <hr />
      <table width="100%" summary="Navigation footer">
        <tr>
          <td width="40%" align="left"><a accesskey="p" href="binit.html">Prev</a> </td>
          <td width="20%" align="center">
            <a accesskey="u" href="OpcodesTop.html">Up</a>
          </td>
          <td width="40%" align="right"> <a accesskey="n" href="biquada.html">Next</a></td>
        </tr>
        <tr>
          <td width="40%" align="left" valign="top">binit </td>
          <td width="20%" align="center">
            <a accesskey="h" href="index.html">Home</a>
          </td>
          <td width="40%" align="right" valign="top"> biquada</td>
        </tr>
      </table>
    </div>
  </body>
</html>
